$(document).ready(function () {
    "use strict";

    //判断是否登录
    isLogin();
	plays();
    //退出登录
    loginOut();

});
var AUTH = {
    role: "",
    job: ""

};
var orgClass='';
var oss = {
    bucket: "vimi8"
};

function plays(){
       orgClass= readCookie("orgClass");
	if(localStorage.jog == "org_corp"){

		window.location = "productManager.html";
		$('#productnav').children().hide();
		$('#productnav').children().eq(1).show();

	}else if(localStorage.job == "org_ebb_city"){

		$('#dr').hide();
		$('#tj').hide();

	}else if(localStorage.job == "org_ebb_county"){

		$('#productadd').hide();
		$('#proth').hide();
		$('#protd').hide();

	}else if(orgClass=="org_ebb_county"){
        $("#audit").hide();
    }else if(orgClass=="org_ebb_city"){
             $("#claim").hide();
              $("#collapseThree2").hide();
         }

}

//获取地址栏参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}

//bootstrap 提示工具
$(function () {
    $("[data-toggle='tooltip']").tooltip();
});



//判断是否登录
function isLogin() {
    var tempRole = readCookie("role");
    var tempJob = readCookie("job");
    if (tempRole && tempRole != "") {
        AUTH.role = tempRole;
        if (tempJob && tempJob != "") {
            AUTH.job = tempJob;
        }
    } else if (window.location.pathname != "/login.html") {
        window.location = "login.html";
    }
}

//写cookie
function writeCookie(name, value, expires_in) {
    console.log("name" + name + "----|----value" + value + "----|----expires_in" + expires_in);
    var expire = new Date((new Date()).getTime() + expires_in);
    expire = "; expires=" + expire.toGMTString();
    document.cookie = name + "=" + encodeURIComponent(value) + expire + "; path=/";//时间段为毫秒数
}

//读取cookie数据
function readCookie(name) {
    var searchName = name + "=";
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        var c = cookies[i];
        while (c.charAt(0) == ' ')
            c = c.substring(1, c.length);
        if (c.indexOf(searchName) == 0)
            return decodeURIComponent(c.substring(searchName.length, c.length));
    }
    return "";
}

//删除cookie
function removeCookie(name) {
    writeCookie(name, '1', -1);
}

//退出登录
function loginOut() {
    $("#logout").on("click", function () {
        $.ajax({
            type: "post",
            url: "/auth/logout/",
            contentType: "application/json",
            success: function (data) {
                removeCookie("role");
                window.location = "login.html";
            }
        });
    });
}


//提示信息
function ShowTip(tip, type, time) {
    var $tip = $('#tip');
    if ($tip.length == 0) {
        $tip = $('<strong id="tip" style="position:fixed;top:30%;left: 50%;z-index:9999;min-width: 200px;text-align: center;"></strong>');
        $('body').append($tip);
    }
    $tip.stop(true).prop('class', 'alert alert-' + type).text(tip).css('margin-left', -$tip.outerWidth() / 2).fadeIn(500).delay(time).fadeOut(500);
}
function fillCounty(addressCode, selectId) {// addressCode
// ebb/geo/?addressCode=520600000000&regionLever=3
    //  value="--选择区域--"
    if (selectId) {
        $("#" + selectId).append("<option value=''>--选择区域--</option>");
        ;
    }

    $.ajax({
        type: "GET",
        url: "/ebb/geo/?addressCode=" + addressCode,
        dataType: "json",
        async:false,
        success: function (data) {
            // console.log("data="+data);
            if (data) {
                $.each(data, function (i, item) {
                    console.log("item :" + item);
                    $("#" + selectId).append("<option value='" + item.id + "'>" + item.geoName + "</option>");
                })
            }
        }
    });
     $("#" + selectId).append("<option value='520600000000'>无对应区县</option>");
}
//加载地址
function loadAddress(addressCode, callback) {
    $.ajax({
        type: "GET",
        url: "/ebb/geo/?addressCode=" + addressCode,
        dataType: "json",
        async: false,
        success: function (data) {
            callback(data);
        }
    });

    //获取当前系统日期
    function  getCurrentDate(){
        var date = new Date();
        var year = date.getFullYear();//年
        var mouth  = date.getMonth()+1;
        var day = date.getDate();
        //判断如果是1-9日期则加上0
        if(mouth>=1 && mouth<=9){
            mouth="0"+mouth;
        }
        if(day>=1 && day<=9){
            day="0"+day;
        }
        var platformDate = year+"-"+mouth+"-"+day;

        return platformDate;
    }

}

function loadPlatformCategory(templateId,type) {
    $("#"+templateId).empty();
    $.ajax({
        type: "get",
        url: "/ebb/platform/category/?type="+type,
        contentType: "application/json",
        success: function (result) {
            $("#"+templateId).append("<option value=\"\">--请选择归属平台--</option>");
            $.each(result,function(i,val){
                $("#"+templateId).append('<option value='+val+'>'+val+'</option>');
            });
        }
    });
}
